﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProcessFile
{
    class ConvertStk4StackupFileStage2
    {

        public string Id = Common.RandomId;
        public string SubId = Common.RandomId;
        public List<string> SubIds=new List<string>();
        private string[] _files;
        public string[] FilesStk { 
            get { return _files; } 
            set { _files = value;
                foreach (string file in _files)
                {
                    SubIds.Add(Common.RandomId);
                }
            } }
        public int StartIdMechanical = AD14LayerId.LAYER_V8_Mechanical1;
        public int Id0 { set; get; }
        public int Id1 { set; get; }
        public string Hearder
        {
            get
            {
                string hearder =
                    string.Format(
                        @"STACKUPVERSION=1
LAYERMASTERSTACK_V8ID={0}
LAYERMASTERSTACK_V8NAME=Master layer stack
LAYERMASTERSTACK_V8ISFLEX=False
LAYERMASTERSTACK_V8SHOWTOPDIELECTRIC=False
LAYERMASTERSTACK_V8SHOWBOTTOMDIELECTRIC=False
LAYERMASTERSTACK_V8STYLE=0
DISPLAYUNIT=1
LAYERMASTERSTACK_V8ID={0}
", Id);
                string subStack = "";
                for (int i = 0; i < FilesStk.Length; i++)
                {
                    
                    subStack += string.Format(@"LAYERSUBSTACK_V8_{2}ID={0}
LAYERSUBSTACK_V8_{2}NAME={1}
LAYERSUBSTACK_V8_{2}ISFLEX={3}
LAYERSUBSTACK_V8_{2}SHOWTOPDIELECTRIC=False
LAYERSUBSTACK_V8_{2}SHOWBOTTOMDIELECTRIC=False
LAYERSUBSTACK_V8_{2}TYPE=1
LAYERSUBSTACK_V8_{2}USEDBYPRIMS={4}
LAYERSUBSTACK_V8_{2}SERVICE=FALSE
", SubIds[i], _files[i], i, i == 1 ? "True" : "False", i == 0 ? "TRUE" : "FALSE");
                }
                string topPaste = string.Format(@"LAYER_V8_0LAYERID={0}
LAYER_V8_0ID={2}
LAYER_V8_0NAME=Top Paste
LAYER_V8_0USEDBYPRIMS=FALSE
LAYER_V8_0_{1}CONTEXT=0
LAYER_V8_0_{1}USEDBYPRIMS=FALSE
", AD14LayerId.LAYER_V8_Top_Paste, SubIds[0],Common.RandomId);
                for (int i = 1; i < FilesStk.Length; i++)
                {
                    topPaste += string.Format(@"LAYER_V8_0_{0}CONTEXT=1
LAYER_V8_0_{0}USEDBYPRIMS=FALSE
", SubIds[i]);
                }
                string topOverlay = string.Format(@"LAYER_V8_1LAYERID={0}
LAYER_V8_1ID={1}
LAYER_V8_1NAME=Top Overlay
LAYER_V8_1USEDBYPRIMS=FALSE
", AD14LayerId.LAYER_V8_Top_Overlay,
                                            Common.RandomId);
                for (int i = 0; i < FilesStk.Length; i++)
                {
                    topOverlay += string.Format(@"LAYER_V8_1_{0}CONTEXT=0
LAYER_V8_1_{0}USEDBYPRIMS=FALSE
", SubIds[i]);
                }

                return hearder + subStack+topPaste+topOverlay;
            }
        }
        public string Footer(int indexStar, int seqStart)
        {
            string bottom =
                string.Format(
                    @"LAYER_V8_{0}LAYERID={1}
LAYER_V8_{0}ID={2}
LAYER_V8_{0}NAME=Bottom Overlay
LAYER_V8_{0}USEDBYPRIMS=FALSE
",
                    indexStar, AD14LayerId.LAYER_V8_Bottom_Overlay, Common.RandomId);
            for (int i = 0; i < FilesStk.Length; i++)
            {
                bottom += string.Format(@"LAYER_V8_{1}_{0}CONTEXT=0
LAYER_V8_{1}_{0}USEDBYPRIMS=FALSE
", SubIds[i],indexStar);
            }
            indexStar++;
            bottom += string.Format(
                @"LAYER_V8_{0}LAYERID={1}
LAYER_V8_{0}ID={2}
LAYER_V8_{0}NAME=Bottom Paste
LAYER_V8_{0}USEDBYPRIMS=FALSE
LAYER_V8_{0}_{3}CONTEXT=0
LAYER_V8_{0}_{3}USEDBYPRIMS=FALSE
",
                indexStar, AD14LayerId.LAYER_V8_Bottom_Paste, Common.RandomId, SubIds[0]);
            for (int i = 1; i < FilesStk.Length; i++)
            {
                bottom += string.Format(@"LAYER_V8_{1}_{0}CONTEXT=1
LAYER_V8_{1}_{0}USEDBYPRIMS=FALSE
", SubIds[i], indexStar);
            }
         
            for (int i = 1; i <= 32; i++)
            {
                string strSub = "";
                for (int j = 0; j < FilesStk.Length; j++)
                {
                    strSub += string.Format(@"LAYER_V8_{1}_{0}CONTEXT=0
LAYER_V8_{1}_{0}USEDBYPRIMS=FALSE
", SubIds[j], indexStar + i);
                }

                bottom +=
                    string.Format(
                        @"LAYER_V8_{0}LAYERID={1}
LAYER_V8_{0}ID={2}
LAYER_V8_{0}NAME=Mechanical {3}
LAYER_V8_{0}USEDBYPRIMS=FALSE
{4}LAYER_V8_{0}MECHENABLED=False
",
                        indexStar + i, StartIdMechanical + (i - 1), Common.RandomId, i, strSub);
            }
            return bottom;
        }
    }
}
